Skip to content

Commit cd10329

Browse files
committed
fix: fix #2
1 parent 086e3fb commit cd10329

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

lib/css-replace.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ function cssReplace (content, replacer) {
3535
throw new Error(`parse rule ${statement} failed`)
3636
}
3737

38+
// add suffix for @import statement
39+
if (!parsed[0].rule.endsWith(';')) {
40+
parsed[0].rule += ';'
41+
}
42+
3843
/**
3944
* parsed[0]: {
4045
* path: 'foobar.css',
4146
* condition: 'print',
42-
* rule: '@import url("foobar.css") print'
47+
* rule: '@import url("foobar.css") print;'
4348
* }
4449
*/
4550
return replacer(Object.assign({
+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import "./refer.css";
2+
13
body {
24
background: url(../img/xxx.png);
35
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
color: #333;
3+
}

test/index.test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ const fs = require('fs')
44
const path = require('path')
55
const assert = require('assert')
66
const webpack = require('webpack')
7+
const cssReplace = require('../lib/css-replace')
78

89
describe('test plugin', function () {
910
this.timeout(60000)
11+
const basedir = path.join(__dirname, 'fixtures/simple')
1012

1113
it('should compile with simple config', function (done) {
12-
const basedir = path.join(__dirname, 'fixtures/simple')
1314
const config = require(path.join(basedir, 'webpack.config.js'))
1415
webpack(config).run((err, stats) => {
1516
if (err) {
@@ -35,4 +36,13 @@ describe('test plugin', function () {
3536
}
3637
})
3738
})
39+
40+
it('should css replace @import correctly', function (done) {
41+
const original = fs.readFileSync(path.join(basedir, 'src/page/index.css'), 'utf-8')
42+
const replaced = cssReplace(original, refer => {
43+
return refer.rule;
44+
})
45+
assert.strictEqual(original, replaced)
46+
done()
47+
})
3848
})

0 commit comments

Comments
 (0)