Skip to content

Commit

Permalink
feat: respect webpack defaults by using fully initialized compiler ob…
Browse files Browse the repository at this point in the history
…ject

This change helps for cases when `useCompilerPath` option is used, but `output.path` is not defined (since it is optional https://webpack.js.org/migrate/5/?_sm_au_=iVVn83n2sDMW0RFrMjpfkK68cqsfp#clean-up-configuration )

Closes: ztoben#403
  • Loading branch information
Den-dp committed May 17, 2021
1 parent 29aa549 commit a3d761a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dist
tmp
npm-debug.log*
.package.json
.idea

# Lock files shouldn't be committed for libraries https://stackoverflow.com/a/40206145
yarn.lock
Expand Down
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ AssetsWebpackPlugin.prototype = {
apply: function (compiler) {
const self = this

self.options.path = path.resolve(
self.options.useCompilerPath
? (compiler.options.output.path || '.')
: (self.options.path || '.')
)
self.writer = createQueuedWriter(createOutputWriter(self.options))
compiler.hooks.initialize.tap('AssetsWebpackPlugin', () => {
self.options.path = path.resolve(
self.options.useCompilerPath
? (compiler.options.output.path || '.')
: (self.options.path || '.')
)
self.writer = createQueuedWriter(createOutputWriter(self.options))
})

const emitPlugin = (compilation, callback) => {
const options = compiler.options
Expand Down
31 changes: 31 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,35 @@ describe('Plugin', function () {

expectOutput(args, done)
})

describe('test compatibility with webpack defaults', function () {
const DEFAULT_WEBPACK_OUTPUT_DIR = path.join(__dirname, '../dist')
const expectDistOutput = require('./utils/expectOutput')(DEFAULT_WEBPACK_OUTPUT_DIR)

beforeEach(function (done) {
rmRf(DEFAULT_WEBPACK_OUTPUT_DIR, done)
})

it('support useCompilerPath without setting output.path', function (done) {
const webpackConfig = {
entry: path.join(__dirname, 'fixtures/one.js'),
plugins: [new Plugin({
useCompilerPath: true
})]
}

const expected = {
main: {
js: 'auto/main.js'
}
}

const args = {
config: webpackConfig,
expected: expected
}

expectDistOutput(args, done)
})
})
})

0 comments on commit a3d761a

Please sign in to comment.