-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: ramdisk option, webpack-plugin-ramdisk implementation #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
25e0932
feat: ramdisk option
shellscape bff050e
feat: round out methodology for ramdisk option, tests
shellscape 4fe3f87
chore: npm audit fix
shellscape 2eccce8
chore: update circle with machine config for ramdisk tests
shellscape a3c05bc
test: make ramdisk test cross platform
shellscape af25102
chore: update for feedback, add path error checking
shellscape c8ad88c
docs: add note about directory removal
shellscape 490e4cc
chore: pick package-lock.json from master
shellscape File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| Copyright © 2018 Andrew Powell | ||
|
|
||
| This Source Code Form is subject to the terms of the Mozilla Public | ||
| License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| The above copyright notice and this permission notice shall be | ||
| included in all copies or substantial portions of this Source Code Form. | ||
| */ | ||
| /* eslint-disable no-param-reassign */ | ||
| const crypto = require('crypto'); | ||
| const { existsSync, symlinkSync } = require('fs'); | ||
| const { basename, join, resolve } = require('path'); | ||
|
|
||
| const isCwd = require('is-path-cwd'); | ||
| const readPkgUp = require('read-pkg-up'); | ||
| const rm = require('rimraf'); | ||
| const { WebpackPluginRamdisk } = require('webpack-plugin-ramdisk'); | ||
|
|
||
| const { PluginExistsError, RamdiskPathError } = require('../errors'); | ||
|
|
||
| module.exports = { | ||
| init(compiler) { | ||
| const hasPlugin = compiler.options.plugins.some( | ||
| (plugin) => plugin instanceof WebpackPluginRamdisk | ||
| ); | ||
|
|
||
| /* istanbul ignore if */ | ||
| if (hasPlugin) { | ||
| this.log.error( | ||
| 'webpack-plugin-serve adds WebpackRamdiskPlugin automatically. Please remove it from your config.' | ||
| ); | ||
| throw new PluginExistsError('WebpackRamdiskPlugin exists in the specified configuration.'); | ||
| } | ||
|
|
||
| const pkg = readPkgUp.sync() || {}; | ||
| const { path } = compiler.options.output; | ||
| const lastSegment = basename(path); | ||
| const errorInfo = `The ramdisk option creates a symlink from \`output.path\`, to the ramdisk build output path, and must remove any existing \`output.path\` to do so.`; | ||
|
|
||
| // if output.path is /batman/batcave, and the user is running the build with wsp from | ||
| // /batman/batcave, then the process will try and delete cwd, which we don't want for a number | ||
| // of reasons | ||
| // console.log('output.path:', resolve(path)); | ||
| // console.log('process.cwd:', process.cwd()); | ||
| if (isCwd(path)) { | ||
| throw new RamdiskPathError( | ||
| `Cannot remove current working directory. ${errorInfo} Please run from another path, or choose a different \`output.path\`.` | ||
| ); | ||
| } | ||
|
|
||
| // if output.path is /batman/batcave, and the compiler context is not set and cwd is | ||
| // /batman/batcave, or the context is the same as output.path, then the process will try and | ||
| // delete the context directory, which probably contains src, configs, etc. throw an error | ||
| // and be overly cautious rather than let the user do something bad. oddly enough, webpack | ||
| // doesn't protect against context === output.path. | ||
| if (resolve(compiler.context) === resolve(path)) { | ||
| throw new RamdiskPathError( | ||
| `Cannot remove ${path}. ${errorInfo} Please set the \`context\` to a another path, choose a different \`output.path\`.` | ||
| ); | ||
| } | ||
|
|
||
| if (!pkg.name) { | ||
| // use md5 for a short hash that'll be consistent between wps starts | ||
| const md5 = crypto.createHash('md5'); | ||
| md5.update(path); | ||
| pkg.name = md5.digest('hex'); | ||
| } | ||
|
|
||
| const newPath = join(pkg.name, lastSegment); | ||
|
|
||
| // clean up the output path in prep for the ramdisk plugin | ||
| compiler.options.output.path = newPath; | ||
|
|
||
| this.log.info(`Ramdisk enabled`); | ||
|
|
||
| const plugin = new WebpackPluginRamdisk({ name: 'wps' }); | ||
| plugin.apply(compiler); | ||
|
|
||
| if (existsSync(path)) { | ||
| rm.sync(path); | ||
| } | ||
|
|
||
| symlinkSync(compiler.options.output.path, path); | ||
shellscape marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.