Skip to content

Commit 1390f4d

Browse files
jimexistCap32
authored andcommitted
Add webpack 4 (#14)
Add webpack 4 support
1 parent 5ca99b5 commit 1390f4d

File tree

6 files changed

+1288
-154
lines changed

6 files changed

+1288
-154
lines changed

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
language: node_js
32

43
node_js:
5-
- 4
6-
- 6
7-
- 8
4+
- "6"
5+
- "8"
6+
- "10"
87

98
install:
109
- npm install

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"mkdirp": "^0.5.1",
5656
"prettier-eslint-cli": "^4.7.1",
5757
"rimraf": "^2.5.4",
58-
"webpack": "^2.4.1"
58+
"webpack": "^4.16.0"
5959
},
6060
"jest": {
6161
"collectCoverageFrom": [

src/index.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,54 @@ const defaultMinimizeConf = {
4444
removeStyleLinkTypeAttributes: true,
4545
};
4646

47+
/**
48+
* Retrieves the public path from the loader options, context.options (webpack <4) or context._compilation (webpack 4+).
49+
* context._compilation is likely to get removed in a future release, so this whole function should be removed then.
50+
* See: https://github.com/peerigon/extract-loader/issues/35
51+
*
52+
* @deprecated
53+
* @param {Object} options - Extract-loader options
54+
* @param {Object} context - Webpack loader context
55+
* @returns {string}
56+
*/
57+
function getPublicPath(options, context) {
58+
const property = 'publicPath';
59+
if (property in options) {
60+
return options[property];
61+
}
62+
if (
63+
context.options &&
64+
context.options.output &&
65+
property in context.options.output
66+
) {
67+
return context.options.output[property];
68+
}
69+
if (
70+
context._compilation &&
71+
context._compilation.outputOptions &&
72+
property in context._compilation.outputOptions
73+
) {
74+
return context._compilation.outputOptions[property];
75+
}
76+
return '';
77+
}
78+
4779
export default function (content) {
4880
this.cacheable && this.cacheable();
4981

5082
const callback = this.async();
51-
const {
52-
options: { context, output, target },
53-
_module = {},
54-
resourcePath,
55-
} = this;
83+
const { context, target, _module = {}, resourcePath } = this;
5684

5785
const options = getOptions(this) || {};
86+
5887
const { resource } = _module;
5988

6089
const hasIssuer = _module.issuer;
6190
const issuerContext = (hasIssuer && _module.issuer.context) || context;
6291

6392
const {
6493
root = resolve(context, issuerContext),
65-
publicPath = output.publicPath || '',
94+
publicPath = getPublicPath(options, this),
6695
enforceRelativePath = false,
6796
format,
6897
transformContent = (content) => {

test/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default (options = {}) => {
66
const { target, globalPublicPath = '/', ...wxmlLoaderOptions } = options;
77
return {
88
entry: resolve(__dirname, 'src', 'index.wxml'),
9+
mode: 'development',
910
output: {
1011
filename: 'index.js',
1112
publicPath: globalPublicPath,

test/test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const clear = () => {
1515
rimraf.sync(resolve(__dirname, 'dist'));
1616
};
1717

18+
const target = (target) => (compiler) =>
19+
compiler.apply(new webpack.LoaderTargetPlugin(target));
20+
1821
const mkdir = () => {
1922
clear();
2023
};
@@ -33,7 +36,7 @@ const compile = (content, options = {}) => {
3336
});
3437
};
3538

36-
describe('wxml-loader', () => {
39+
describe('wxml-loader', async () => {
3740
beforeEach(mkdir);
3841
afterEach(clear);
3942

@@ -72,7 +75,7 @@ describe('wxml-loader', () => {
7275
test('should Wechat target work', async () => {
7376
await compile(
7477
'<import src="/fixture.wxml" /><view wx:for="{{items}}">{{item}}</view>',
75-
{ target: function Wechat() {} },
78+
{ target: target(function Wechat() {}) },
7679
);
7780
expect(getCompiledRes()).toBe(
7881
'<import src="/fixture.wxml" /><view wx:for="{{items}}">{{item}}</view>',
@@ -82,7 +85,7 @@ describe('wxml-loader', () => {
8285
test('should Alipay target work', async () => {
8386
await compile(
8487
'<import src="/fixture.wxml" /><view wx:for="{{items}}">{{item}}</view>',
85-
{ target: function Alipay() {} },
88+
{ target: target(function Alipay() {}) },
8689
);
8790
expect(getCompiledRes()).toBe(
8891
'<import src="/fixture.axml" /><view a:for="{{items}}">{{item}}</view>',
@@ -91,7 +94,7 @@ describe('wxml-loader', () => {
9194

9295
test('should transformContent() work', async () => {
9396
await compile('<view wx:for="{{items}}"> {{item}} </view>', {
94-
target: function Alipay() {},
97+
target: target(function Alipay() {}),
9598
transformContent: (content) => content.replace(/\bwx:/, '🦄:'),
9699
});
97100
expect(getCompiledRes()).toBe('<view 🦄:for="{{items}}"> {{item}} </view>');
@@ -100,15 +103,15 @@ describe('wxml-loader', () => {
100103
// DEPRECATED
101104
test('should format() work', async () => {
102105
await compile('<view wx:for="{{items}}"> {{item}} </view>', {
103-
target: function Alipay() {},
106+
target: target(function Alipay() {}),
104107
format: (content) => content.replace(/\bwx:/, '🦄:'),
105108
});
106109
expect(getCompiledRes()).toBe('<view 🦄:for="{{items}}"> {{item}} </view>');
107110
});
108111

109112
test('should transformUrl() work', async () => {
110113
await compile('<import src="/fixture.wxml" />', {
111-
target: function Alipay() {},
114+
target: target(function Alipay() {}),
112115
transformUrl: (url) => url.replace(/fixture/, '🦄'),
113116
});
114117
expect(getCompiledRes()).toBe('<import src="/🦄.wxml" />');

0 commit comments

Comments
 (0)